#!/usr/bin/python
# -*- coding: utf-8 -*-
# @author: 高建平
# @description: 
#
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from config.database import settings

SQLALCHEMY_DATABASE_URL = "mysql+pymysql://{user}:{password}@{host}:{port}/{database}"

engine = create_engine(
    SQLALCHEMY_DATABASE_URL.format(user=settings.USER,password=settings.PASSWORD,host=settings.HOST,port=settings.PORT,database=settings.DATABASE),
    echo=True,
    pool_size = 5,
    max_overflow = 20,
    pool_timeout = 30,
    pool_recycle = 3600,
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

Base = declarative_base()


# Dependency
def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()
